<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<!--<link href="../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">-->
		<link rel="stylesheet" type="text/css" href="../../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
		<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />-->
		<link rel="stylesheet" type="text/css" href="../../../css/footer.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/foods_detail.css" />
		<script src="../../../js/flexible.min.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="wrap">
			<div id="main" style="padding-top: 1.3rem;">
				<div id="header">
					<a id="headImg" href="javascript:history.go(-1)"><img class="headImg" src="../../../images/12.png" /></a>
					<p id="headTitle">商品详情</p>
				</div>
				<div id="content">
					<div class="foods_detail_wap">
						<div class="foods_detail_pic">
							<img src="" />
						</div>
						<div class="foods_detail_words">
							<div class="foods_detail_words_l">
								<img src="" />
							</div>
							<div class="foods_detail_words_r">
								<h3 id="shopName"></h3>
								<p>积分: <span id="shopPrice"></span> </p>
								<p>数量&nbsp;:&nbsp;<span id="jianNum" style="border: 1px solid #ccc;">&nbsp;-&nbsp;</span><label id="shopNum" style="width: 20px;text-align: center;">1</label><span id="addNum" style="border: 1px solid #ccc;">&nbsp;+&nbsp;</span></p>
								<p>邮费&nbsp;:&nbsp;包邮</p>
							</div>
						</div>
						<div class="explain">
							<div class="xian" style="display: none;">
								<h3>图文说明</h3>
							</div>
							<ul class="explain_pic" style="display: none;">
								<li id="shopPicinfo">
									<img src="" />
								</li>
								<!--<li>
									<img src="../../../images/coalltion/mianbao.jpg"/>
								</li>-->
							</ul>
							<a id="panduan" href="#">积分兑换</a>
						</div>
					</div>
				</div>
			</div>

		</div>

		<div class="duihuan_box">
			<div class="duihuan">
				<div class="duihuan_top">
					商品兑换？
				</div>
				<div class="duihuan_down">
					<a id="Besure">确定</a>
					<a href="#">取消</a>
				</div>
			</div>
		</div>

	</body>
	<script src="../../../js/jquery-2.2.3.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/foods_detail.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var tokenReztApp = $.cookie("tokenReztApp");
		if(!tokenReztApp) {
			window.location.href = "../../login/login/login.html";
		}
		$(function() {
			currentyf();
			canshu();
		});

		//用户信息
		var user, role, goodsId;
		//获取当前会员信息
		function currentyf() {
			$.ajax({
				cache: false,
				type: "GET",
				url: "http://47.104.13.109/member-service/auth/current?token=" + tokenReztApp,
				dataType: "JSON",
				async: false,
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$.removeCookie('tokenReztApp', {
						expires: 7,
						path: '/'
					});
					$.removeCookie("tokenRh", {
						expires: 7,
						path: '/'
					});
					$.removeCookie('memberId', {
						expires: 7,
						path: '/'
					});
					$.removeCookie('roleId', {
						expires: 7,
						path: '/'
					});
					window.location.href = "../../login/login/login.html";
				},
				success: function(data) {
					user = data.loginName;
					role = data.roleList[0].id; //角色
					goodsId = data.id;

				}
			});
		}
		
		var goldscale;
		function canshu(){
			$.ajax({
				cache: false,
				type: "GET",
				url: "http://47.104.13.109/order-service/selectall",
				data: {
						"id": "1",
						"pageNum": "1",
						"pageSize": "100"
					 },
				dataType: "JSON",
				async: false,
				error: function(XMLHttpRequest, textStatus, errorThrown) {
				},
				success: function(data) {
					console.log(data);
					console.log(data.list[0].parameter);
					goldscale = data.list[0].parameter;

				}
			});
		}

		function UrlSearch() {
			var name, value;
			var str = location.href; //取得整个地址栏
			var num = str.indexOf("?")
			str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]

			var arr = str.split("&"); //各个参数放到数组里
			for(var i = 0; i < arr.length; i++) {
				num = arr[i].indexOf("=");
				if(num > 0) {
					name = arr[i].substring(0, num);
					value = arr[i].substr(num + 1);
					this[name] = value;
				}
			}
		}
		var Request = new UrlSearch();
		//		alert(Request);
		//		if(typeof(Request.uid)!='undefined' && typeof(Request.keys)!='undefined'){
		//			$.cookie('user_id', Request.uid);
		//			$.cookie('user_key',Request.keys);
		//			$('.navbar-fixed-bottom').hide();
		//		}else{
		//			$('.navbar-fixed-bottom').show();
		//		}
		if(typeof(Request.sid) != 'undefined') {
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/product-service/selectOne?goodsId=" + Request.sid,
				async: true,
				success: function(data) {
					console.log(data);
					$('#shopName').html(data[0].goodsName);
					//					$('#shopNum').html(data[0].goodsNum);
					$('#shopPrice').html(data[0].goodsIntegral);
					$('#shopPicinfo').html('<img src="' + data[0].goodsIntro + '"/>');
					$('.foods_detail_pic').html('<img src="' + data[0].goodsPic + '"/>')

				}
			});
		}
		$("#addNum").click(function() {
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/product-service/selectOne?goodsId=" + Request.sid,
				async: true,
				success: function(data) {
					console.log(data);
					$('#shopPrice').html(data[0].goodsIntegral * $('#shopNum').html());

				}
			})
		})

		$("#jianNum").click(function() {
			if($('#shopNum').html() == 0) {
				$('#shopNum').html('01')
			}
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/product-service/selectOne?goodsId=" + Request.sid,
				async: true,
				success: function(data) {
					console.log(data);
					$('#shopPrice').html(data[0].goodsIntegral * $('#shopNum').html());

				}
			})
		})

		$("#Besure").on("click", function() {
			$("#Besure").unbind("click")
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/member-service/auth/current?token=" + $.cookie("tokenRh"),
				async: false,
				success: function(res) {
					for(var i = 0; i < res.roleList.length; i++) {
						if(res.roleList[i].name == "粉丝") {
							var part = "粉丝";
						} else if(res.roleList[i].name == "会员") {
							var part = "会员";
						} else if(res.roleList[i].name == "创客") {
							var part = "创客";
						}

					}
					console.log(res)
					$.ajax({
						type: "get",
						url: "http://47.104.13.109/integral-service/tfIntegral/" + res.id,
						async: false,
						success: function(inte) {
							console.log(inte.integralNum)
								//						if (true) {
							if(inte.integralNum >= $("#shopPrice").html()) {
								$.ajax({
									type: "get",
									url: "http://47.104.13.109/product-service/selectOne?goodsId=" + Request.sid,
									async: false,
									success: function(data) {
										console.log(data);
										console.log(data["0"].goodsIntegral)
										console.log(40 * 01)
											//生成订单号
										$.ajax({
											type: "get",
											url: "http://47.104.13.109:80/cart-service/ordersn",
											async: true,
											success: function(data1) {
												console.log(data1)

												//创建购物车
												$.ajax({
													type: "post",
													url: "http://47.104.13.109/cart-service/add",
													async: false,
													data: {
														userId: res.id,
														userName: res.loginName,
														goodsId: data["0"].goodsId,
														goodsNum: data["0"].goodsNum,
														goodsName: data["0"].goodsName,
														goodsIntegral: $("#shopPrice").html(),
														goodsCatid: data["0"].goodsCatid,
														goodsPic: data["0"].goodsPic,
														goodsIntro: data["0"].goodsIntro,
														goodsCount: $("#shopNum").html(),
														goldscale:goldscale,
														orderSn: data1,
													},
													success: function(aa) {
														console.log(aa);

														//创建定单
														$.ajax({
															type: "post",
															url: 'http://47.104.13.109/order-service/save?goodsId=' + data["0"].goodsId + '&goodsName=' + data["0"].goodsName + '&goodsIntegral=' + $('#shopPrice').html() + '&goodsCatid=' + data["0"].goodsCatid + '&goodsPic=' + data["0"].goodsPic + '&goodsIntro=' + data["0"].goodsIntro + '&goodsCount=' + $("#shopNum").html() + '&userAccount=' + res.loginName + '&userId=' + res.id + '&userName=' + res.loginName + '&userIdentity=' + part + '&userPhone=' + res.tel + '&userAddress=' + res.address + '&goosGold=' + $("#shopPrice").html() * goldscale + '&orderSn=' + data1,
															async: false,
															success: function(aa) {
																console.log(aa)

																$.ajax({
																	type: "put",
																	url: "http://47.104.13.109/information-service/information/send",
																	async: true,
																	data: {
																		inforSendperson: '系统管理员',
																		inforContent: '您在礼享商城成功兑换商品' + data["0"].goodsName + ', 消费积分：' + $('#shopPrice').html() + '祝你购物愉快~~',
																		inforReceiveperson: $.cookie("memberId"),
																	},
																	success: function(res3) {
																		console.log(res3)
																		console.log($("#shopPrice").html() * 0.01);
																		window.location.href = "../gift_sell/gift_sell.html?goodsId=" + Request.sid + '&orderSn=' + data1;
																		//											$("#Besure").attr('disabled',false);
																		return false;
																	},
																	error: function(error) {
																		$("#Besure").attr('disabled', false);
																	}

																});
																//									return false;

																//								},error:function(error){
																//									$("#Besure").attr('disabled',false);
																//								}
																//								
																//							});

															},
															error: function(error) {
																$("#Besure").attr('disabled', false);
																alert("生成订单时发生错误");
															}
														});

													},
													error: function(error) {
														$("#Besure").attr('disabled', false);
														alert("出现错误，请联系系统管理员")

													},
													error: function(error) {
														$("#Besure").attr('disabled', false);
														alert("出现错误，请联系系统管理员")
													}
												});
											},
											error: function(error) {
												alert("创建订单号时失败")
											}
										});

									},
									error: function(error) {
										$("#Besure").attr('disabled', false);
										alert("出现错误，请联系系统管理员")
									}
								});
							} else {
								alert("您的积分不足");
							}
						}
					});
				},
				error: function(error) {
					$("#Besure").attr('disabled', false);
					alert("出现错误，请联系系统管理员")
				}
			});
		})
	</script>

</html>